def sort2(line_decode):
    length = 0
    for i in range(len(line_decode)):
        i_ = line_decode[i:i + 1]
        encode = i_.encode('unicode_escape')
        min = '\u4e00'.encode('unicode_escape')
        max = '\u9fa5'.encode('unicode_escape')
        if (min <= encode) & (encode <= max):
            length += 2
        else:
            length += 1
    return length


def sort():
    dir = 'D:\\Obsidian\\obsidianblog\\娱乐\\娱乐'
    file = dir + '\\video.md'
    newfile = dir + '\\newvideo.md'
    up = []
    with open(file, 'rb') as f:
        while True:
            line = f.readline()
            if line:
                line_decode = line.decode('utf-8')
                if line_decode.endswith('\n'):
                    line_decode = line_decode[:-1]
                    if line_decode.endswith('\r'):
                        line_decode = line_decode[:-1]
                # length = sort2(line_decode)
                # print(line_decode + str(length))
                if len(line_decode) > 0:
                    up.append(line_decode + '\n')
            else:
                break
    # up = sorted(up, key=lambda i: sort2(i), reverse=False)
    up.sort(key=sort2, reverse=False)
    print(up)
    with open(newfile, 'w+', encoding='utf-8') as w:
        w.writelines(up)


if __name__ == '__main__':
    sort()
